{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>省级{:lang('list')}</legend>
    </fieldset>
    <div class="demoTable">
        <div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">省级名称</label>
				<div class="layui-input-inline layui-form">
					<input class="layui-input" name="key" id="key" placeholder="请输入省级名称">
				</div>
			</div>
        </div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">{:lang('status')}</label>
				<div class="layui-input-inline layui-form">
					<select id="open">
						<option value=""></option>
						{volist name="open" id="open"}
						<option value="{$key}">{$open}</option>
						{/volist}
					</select>
				</div>
			</div>		
		</div>
        
        
    </div>
	<div class="demoTable layui-row">
		<button class="layui-btn" style="margin-left:30px;margin-top:15px;" id="search" data-type="reload">{:lang('search')}</button>
		<a href="{:url('index')}" style="margin-top:15px;" class="layui-btn">{:lang('reset')}</a>
	</div>
	<blockquote class="layui-elem-quote">
        <a href="{:url('add')}" class="layui-btn layui-btn-small">添加省级</a>
    </blockquote>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
<script type="text/html" id="status">
	<input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="{:lang('enabled')}|{:lang('disabled')}" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="action">
	 <a href="{:url('edit')}?id={{d.id}}" class="layui-btn layui-btn-xs">{:lang('edit')}</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('del')}</a>
</script>
{include file="common/foot"/}
<script>
     layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form,$ = layui.jquery;
        var tableIn = table.render({
            id: 'ad',
			elem: '#list',
            url: '{:url("index")}',
            method: 'post',
            page: true,
            cols: [[
                {field: 'id',sort: true,align: 'center', title: '{:lang("id")}', width: 80, fixed: true},
                {field: 'name',align: 'center', title: '省级名称', width: 300},
				{field: 'status', align: 'center', title: '{:lang("status")}', width: 100, toolbar: '#status'},
                {width: 200, align: 'center',title: '{:lang("action")}', toolbar: '#action'}
            ]],
            limit: '' //每页默认显示的数量
        });
        //搜索
        $('#search').on('click', function(){
            var key = $('#key').val();
			var status  = $('#open').val();
            if($.trim(status) === '' && $.trim(key)==='') {
                layer.msg('{:lang("query_condition")}',{icon:0});
                return;
            }
            tableIn.reload({
                where: {key: key,status:status}
            });
        });
		form.on('switch(status)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var status = obj.elem.checked===true?1:0;
            $.post('{:url("editState")}',{'id':id,'status':status},function (res) {
                layer.close(loading);
				console.log(res);
                if (res.errorCode == 8888){
						layer.msg(res.msg,{time:1000,icon:1});
						//tableIn.reload();
                    }else{
                        layer.msg(res.msg,{time:1000,icon:2},function(){
							location.href = res.url;
							});
                        return false;
                    }

            })
        });	
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'del') {
				//单个删除方法
                layer.confirm('确定要删除该省份吗（确认将会删除该省下面的所有市区县）？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('del')}",{id:data.id},function(res){
                        layer.close(loading);
						//console.log(res);
                        if(res.errorCode == 8888){
                            layer.msg(res.msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });

        
    });
</script>
</body>
</html>